﻿package
	{
	import flash.events.Event;
	
	public class Vehicule extends Player
		{
		var wheelAngle:Number = 0;
		var Xtarget:Number = 0;
		var Ytarget:Number = 0;
		
		public function Vehicule(keyHandler:Object)
			{
			super(keyHandler);
			maxSpeed = 40;
			resist = 0.9;
			accel = 0.02;
			}
		
		override public function run(e:Event)
			{
			this.computeMove();
			}
		
		public function setAngle()
			{
			var angle:Number = rotation * MainClass.toRadian;
			this.Xtarget = Math.cos(angle)*this.curSpeed;
			this.Ytarget = Math.sin(angle)*this.curSpeed;
			var coef = 2;
			
			if (Xtarget!=Xspeed)
				Xspeed += (Xtarget>Xspeed)?coef:-coef;
			if (Ytarget!=Yspeed)
				Yspeed += (Ytarget>Yspeed)?coef:-coef;
			}
		
		override public function computeMove()
			{
			var hor:Boolean = arrowPressed[37] || arrowPressed[39];
			var ver:Boolean = arrowPressed[38] || arrowPressed[40];
			moving = ver;
			
			if (arrowPressed[39]) 
				{
				if (wheelAngle<=0.1)
					wheelAngle += 0.01;
				}
			else if (arrowPressed[37])
				{
				if (wheelAngle>=-0.1)
					wheelAngle -= 0.01;
				
				}
			else
				{
				if (wheelAngle>0)
					wheelAngle -= 0.01;
				else if (wheelAngle<0)
					wheelAngle += 0.01;
				}
			
			if (arrowPressed[40])
				{
				accel=(accel<0)?accel:-accel;
				}
			else if (arrowPressed[38])
				{
				accel=(accel>0)?accel:-accel;
				}
			
			
			setAngle();
			
			
			if (moving)
				{
				this.rotation += wheelAngle*curSpeed;
				if (Math.abs(curSpeed) < maxSpeed)
					curSpeed += maxSpeed * accel;
				else
					{
					curSpeed = (accel>0)?maxSpeed:-maxSpeed;
					}
				}
			else
				{
				if (Math.abs(curSpeed) > 1)
					{
					curSpeed *= resist;
					this.rotation += wheelAngle*curSpeed;
					}
				else
					curSpeed = 0;
				}
			
			/*
			if (Math.abs(Xspeed) > 1) Xspeed *= resist;
			else Xspeed = 0;
		
			if (Math.abs(Yspeed) > 1) Yspeed *= resist;
			else Yspeed= 0;*/
			
			if (!this.passive)
				{
				parent.x -= Xspeed*parent.scaleX;
				parent.y -= Yspeed*parent.scaleY;
				}
			
			this.x += Xspeed;
			this.y += Yspeed;
			
			}
		
		
		}
		
	}